﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static h1.Form1;
using System.Xml.Serialization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace h1
{
    public partial class Form_user : Form
    {
        string select;
        public bool confirm;
        Form1 f1;
        Form_list f2;
        string id;
        string skin_user;
        public Form_user()
        {
            InitializeComponent();
        }


        ////界面换肤
        //public static Sunisoft.IrisSkin.SkinEngine SkinEngine = new Sunisoft.IrisSkin.SkinEngine();
        //public static List<string> Skins;
        public Form_user(Form1 f1 ,Form_list f2, string select)
        {
            InitializeComponent();
            this.select = select;
            this.f1 = f1;
            this.f2 = f2;
           
            //加载所有皮肤列表
            Form1.Skins = Directory.GetFiles(Application.StartupPath + @"\Skins\", "*.ssk").ToList();
            skin_option.Text = "请选择主题";
            Form1.Skins.ForEach(x =>
            {
                skin_option.Items.Add(Path.GetFileNameWithoutExtension(x));
            });
            skin_option.Items.Add("默认");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form_confirm form_Confirm = new Form_confirm(this,$"正准备退出登录账号：\n用户名-{label11.Text}\n用户ID-{label12.Text}\n【提示】：退出登录后需要重新登陆！\n且会取消自动登陆！","是否确认退出登录？");
            form_Confirm.ShowDialog();
            if (confirm)
            {
                //denglu_cancelZidong();
                this.Hide();
                f2.Close();
                f1.denglu_cancelZidong();
                Form1 form1 = new Form1();
                form1.ShowDialog();
                this.Close();
            }
        }
        public void denglu_cancelZidong()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(accout));
                string xmlFileName = "zidongData.xml";
                FileStream fs = new FileStream(xmlFileName, FileMode.Create);
                xmlSerializer.Serialize(fs, new accout(0, "", ""));
                fs.Close();
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("账号(取消自动登录)序列化错误", "错误描述：" + ex.Message);
                fm.ShowDialog();
            }
        }
        private void Form_user_Load(object sender, EventArgs e)
        {
            DataSet ds_user = MySqlHelper.getDataSet(MySqlHelper.Conn, "SELECT * FROM users");
            DataRow[] rows = ds_user.Tables[0].Select(select);
            if (rows.Count() < 1)
            {
                Form_message form_Message = new Form_message("数据加载失败！","没有找到该用户！");
                form_Message.ShowDialog();
            }
            else
            {
                label1.Text = "用户名：" + rows[0]["userName"].ToString();
                label2.Text = "用户ID：" + rows[0]["userID"].ToString();
                id= rows[0]["userID"].ToString();
                label11.Text = rows[0]["userName"].ToString();
                label12.Text = rows[0]["userID"].ToString();
                label13.Text = (rows[0]["userGender"].ToString()=="")?"未设置":rows[0]["userGender"].ToString();
                label16.Text = (rows[0]["userBirthday"].ToString()=="")?"未设置": rows[0]["userBirthday"].ToString();
                label15.Text = (rows[0]["userBirthday"].ToString() == "") ? "未设置" : rows[0]["userBirthday"].ToString();
                label14.Text = (rows[0]["userBirthday"].ToString() == "") ? "未设置" : rows[0]["userBirthday"].ToString();
                skin_option.SelectedItem = rows[0]["userSkin"].ToString();
                skin_user = rows[0]["userSkin"].ToString();
                if (rows[0]["userQuestion"].ToString() == "")
                {
                    label18.Text = "否";
                    label19.Visible = true;
                    button5.Visible = false;
                }
                else
                {
                    label18.Text = "是";
                    label19.Visible = false;
                    button5.Visible = true;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form_setMibao fm = new Form_setMibao(id);
            fm.ShowDialog();
            this.Hide(); 
            Form_user fu = new Form_user(f1,f2,select);
            fu.ShowDialog();
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataSet ds_user = MySqlHelper.getDataSet(MySqlHelper.Conn, "SELECT * FROM users");
            DataRow[] rows = ds_user.Tables[0].Select(select);
            if (rows.Count() < 1)
            {
                Form_message form_Message = new Form_message("数据加载失败！", "没有找到该用户！");
                form_Message.ShowDialog();
            }
            else
            {
                Form_message fm2 = new Form_message("当前密保信息为：", $"【密保问题】：{rows[0]["userQuestion"].ToString()}\n【密保提示】：{rows[0]["userNote"].ToString()}\n【密保答案】：{rows[0]["userAnswere"].ToString()}", id, 0);
                fm2.ShowDialog();
            }
        }

        private void skin_option_SelectedIndexChanged(object sender, EventArgs e)
        {
            //选择皮肤并使用
            if (skin_option.SelectedItem != null && skin_option.SelectedItem != "默认")
            {
                //加载皮肤
                Form1.skinEngine.SkinFile = Form1.Skins[skin_option.SelectedIndex];
                Form1.skinEngine.Active = true;
            }
            else if (skin_option.SelectedItem == "默认")
            {
                Form1.skinEngine.Active = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult expec = System.Windows.Forms.MessageBox.Show("该功能还未实装，敬请期待！", "提示", MessageBoxButtons.OK);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DataSet ds = MySqlHelper.getDataSet(MySqlHelper.Conn, $"SELECT * FROM users WHERE userID = '{id}'");
            ds.Tables[0].Rows[0]["userSkin"] = skin_option.SelectedItem;
            MySqlHelper.updateDataSet(MySqlHelper.Conn, $"SELECT * FROM users WHERE userID = '{id}'", ds);
            DialogResult expec = System.Windows.Forms.MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            skin_option.SelectedItem = skin_user;
            if (skin_option.SelectedItem != null && skin_option.SelectedItem != "默认")
            {
                //加载皮肤
                Form1.skinEngine.SkinFile = Form1.Skins[skin_option.SelectedIndex];
                Form1.skinEngine.Active = true;
            }
            else if (skin_option.SelectedItem == "默认")
            {
                Form1.skinEngine.Active = false;
            }
        }

        
    }
}
